package com.maddox.mworld.ui.crash;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Process;

import androidx.annotation.NonNull;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * 异常处理
 */
@SuppressWarnings("all")
public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static final String TAG = "CrashHandler";

    private Context mContext;
    private static final String FILE_NAME = "_crash";
    private static final String FILE_NAME_SUFFIX = ".trace";
    private static CrashHandler sInstance = new CrashHandler();
    private Thread.UncaughtExceptionHandler mDefaultCrashHandler;

    private SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd");

    private CrashHandler() {

    }

    public static CrashHandler getInstance() {
        return sInstance;
    }

    public void init(Context context) {
        this.mContext = context.getApplicationContext();
        mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);

    }

    @Override
    public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
        try {
            //异常问题
            writeExceptionToFile(e);
            //上传信息到服务器
            uploadException();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        e.printStackTrace();

        if (mDefaultCrashHandler != null) {
            mDefaultCrashHandler.uncaughtException(t, e);
        } else {
            Process.killProcess(Process.myPid());
        }



    }
    private void uploadException() {
        //上传异常信息到服务器

    }
    private void writeExceptionToFile(Throwable e) throws Exception{
        File filesDir = mContext.getExternalFilesDir(null);
        File logDir = new File(filesDir,"logs");
        if (!logDir.exists()){
            logDir.mkdir();
        }
        String fileName = sdfDate.format(new Date()).concat(FILE_NAME).concat(FILE_NAME_SUFFIX);
        File logFile = new File(logDir,fileName);
        if (!logFile.exists()){
            logFile.createNewFile();
        }
        PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter(logFile,true)));
        printWriter.println();
        printWriter.println("***************"+sdfTime.format(new Date())+"*************");
        dumpPhoneOInfo(printWriter);
        e.printStackTrace(printWriter);
        printWriter.flush();
        printWriter.close();
    }

    private void dumpPhoneOInfo(PrintWriter printWriter) throws Exception {
        PackageManager pm = mContext.getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
        printWriter.print("App Version: ");
        printWriter.print(packageInfo.versionName);
        printWriter.print("_");
        printWriter.println(packageInfo.versionCode);
        //Android 版本号
        printWriter.print("OS Version: ");
        printWriter.print(Build.VERSION.RELEASE);
        printWriter.print("_");
        printWriter.println(Build.VERSION.SDK_INT);
        //手机制造商
        printWriter.print("Vendor: ");
        printWriter.println(Build.MANUFACTURER);
        //手机型号
        printWriter.print("Model: ");
        printWriter.println(Build.MODEL);
        //Cpu架构
        printWriter.print("CPU ABI: ");
        printWriter.println(Arrays.toString(Build.SUPPORTED_ABIS));
    }
}

